package com.hcb.tonguetiphealth.widget;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.hcb.tonguetiphealth.view.DIYActivity;
import com.hcb.tonguetiphealth.view.LoginActivity;
import com.hcb.tonguetiphealth.view.MoreActivity;
import com.hcb.tonguetiphealth.view.R;
import com.hcb.tonguetiphealth.view.ResBookActivity;
import com.hcb.tonguetiphealth.view.SearchActivity;
import com.hcb.tonguetiphealth.widget.GeneralWidget;

public class BottomWidget  implements OnClickListener {

	private Activity ctx;
	private LinearLayout llCity, llSearch, llHotRes, llLogin, llMore;
	private ImageView imgCity, imgSearch, imgHotRes, imgLogin, imgMore;
	private ViewGroup rootView;

	/**
	 * 封装底部按钮的控件绑定以及响应事件；
	 * @param context：activity上下文
	 * @param root：activity 根view
	 */
	public BottomWidget(Context context, ViewGroup root) {
		ctx = (Activity)context;
		rootView = root;
		bindView();
		bindEvent();
	}
	
	public BottomWidget(Context context){
		ctx = (Activity)context;
		bindView2();
		bindEvent();
	}
	
	private void bindView(){
		llCity = (LinearLayout) rootView.findViewById(R.id.linearlayout_home_city);
		llSearch = (LinearLayout) rootView.findViewById(R.id.linearlayout_home_search);
		llHotRes = (LinearLayout) rootView.findViewById(R.id.linearlayout_home_res);
		llLogin = (LinearLayout) rootView.findViewById(R.id.linearlayout_home_login);
		llMore = (LinearLayout) rootView.findViewById(R.id.linearlayout_home_more);
		
		//底部按钮
		imgCity = (ImageView)rootView.findViewById(R.id.img_home_city);
		imgSearch = (ImageView)rootView.findViewById(R.id.img_home_search);
		imgHotRes = (ImageView)rootView.findViewById(R.id.img_home_hotres);
		imgLogin = (ImageView)rootView.findViewById(R.id.img_home_login);
		imgMore = (ImageView)rootView.findViewById(R.id.img_home_more);

	}
	
	private void bindView2(){
		llCity = (LinearLayout) ctx.findViewById(R.id.linearlayout_home_city);
		llSearch = (LinearLayout) ctx.findViewById(R.id.linearlayout_home_search);
		llHotRes = (LinearLayout) ctx.findViewById(R.id.linearlayout_home_res);
		llLogin = (LinearLayout) ctx.findViewById(R.id.linearlayout_home_login);
		llMore = (LinearLayout) ctx.findViewById(R.id.linearlayout_home_more);
		
		//底部按钮
		imgCity = (ImageView)ctx.findViewById(R.id.img_home_city);
		imgSearch = (ImageView)ctx.findViewById(R.id.img_home_search);
		imgHotRes = (ImageView)ctx.findViewById(R.id.img_home_hotres);
		imgLogin = (ImageView)ctx.findViewById(R.id.img_home_login);
		imgMore = (ImageView)ctx.findViewById(R.id.img_home_more);

	}
	
	private void bindEvent(){
		
		llCity.setOnClickListener(this);
		llSearch.setOnClickListener(this);
		llHotRes.setOnClickListener(this);
		llLogin.setOnClickListener(this);
		llMore.setOnClickListener(this);
		
		//底部按钮
		bindTouchEvent(llCity, imgCity);
		bindTouchEvent(llSearch , imgSearch);
		bindTouchEvent(llHotRes, imgHotRes);
		bindTouchEvent(llLogin, imgLogin);
		bindTouchEvent(llMore,imgMore);
	}
	
	/**
	 * 按钮触碰事件;touch后修改背景以及图标
	 */
	private void bindTouchEvent(final LinearLayout linearLayout, final ImageView img) {
		linearLayout.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == KeyEvent.ACTION_DOWN) {
					linearLayout.setBackgroundResource(R.drawable.bottom_menu_bg);
					switch (img.getId()) {
 					case R.id.img_home_city:
 						img.setBackgroundResource(R.drawable.diy_yellow);
						break;
					case R.id.img_home_search:
						img.setBackgroundResource(R.drawable.search_yellow);
						break;
					case R.id.img_home_hotres:
						img.setBackgroundResource(R.drawable.hotres_yellow);
						break;
					case R.id.img_home_login:
						img.setBackgroundResource(R.drawable.login_yellow);
						break;
					case R.id.img_home_more:
						img.setBackgroundResource(R.drawable.more_yellow);
						break;
					default:
						break;
					}
				} else if (event.getAction() == KeyEvent.ACTION_UP) {
					linearLayout.setBackgroundResource(R.drawable.main_gray_bg);
					switch (img.getId()) {
 					case R.id.img_home_city:
 						img.setBackgroundResource(R.drawable.diy_red);
						break;
					case R.id.img_home_search:
						img.setBackgroundResource(R.drawable.search_red);
						break;
					case R.id.img_home_hotres:
						img.setBackgroundResource(R.drawable.hotres_red);
						break;
					case R.id.img_home_login:
						img.setBackgroundResource(R.drawable.login_red);
						break;
					case R.id.img_home_more:
						img.setBackgroundResource(R.drawable.more_red);
						break;
					default:
						break;
					}
				}
				return false;
			}
		});
	}
	
	
	/**
	 * 底部功能菜单按钮点击事件
	 */
	public void onClick(View v) {
		Intent intent = new Intent();
		switch (v.getId()) {
		case R.id.linearlayout_home_city:
			intent.setClass(ctx, DIYActivity.class);
			ctx.startActivity(intent);
			ctx.finish();
			break;
		case R.id.linearlayout_home_search:
			intent.setClass(ctx, SearchActivity.class);
			ctx.startActivity(intent);
			ctx.finish();
			break;
		case R.id.linearlayout_home_res:
			intent.setClass(ctx, ResBookActivity.class);
			ctx.startActivity(intent);
			ctx.finish();
			break;
		case R.id.linearlayout_home_login:
			intent.setClass(ctx, LoginActivity.class);
			ctx.startActivity(intent);
			ctx.finish();
			break;
		case R.id.linearlayout_home_more:
			intent.setClass(ctx, MoreActivity.class);
			ctx.startActivity(intent);
			ctx.finish();
			break;
			//底部按钮
		default:
			break;
		}
	}

}
